package com.colorlight.cloud.complete.entity.enumerate;

import lombok.Getter;

import java.io.Serializable;
import java.util.*;

/**
 * @author Demon
 * @description  int 与周期的转化
 * @date 22/12/2023 下午4:27 周期定义
 */
@Getter
public enum Weekday implements Serializable {
  /**
   * 周期
   * */
  MONDAY(1 << 6, "Monday"),
  TUESDAY(1 << 5, "Tuesday"),
  WEDNESDAY(1 << 4, "Wednesday"),
  THURSDAY(1 << 3, "Thursday"),
  FRIDAY(1 << 2, "Friday"),
  SATURDAY(1 << 1, "Saturday"),
  SUNDAY(1, "Sunday");
  private Integer value;
  private String name;
	private static final long serialVersionUID = 741963852741963852L;
  Weekday(int i, String name) {
    this.value = i;
    this.name = name;
  }
	
	/**
	 * @param value
	 * @return 对应的类型
	 **/
	public static List<Weekday> getWeekday(Integer value) {
		Objects.requireNonNull(value, "value can not be null.");
		
		List<Weekday> weekdays= new ArrayList<>(7);
		
		Arrays.stream(Weekday.values()).forEach(e->{
			if(0 != (e.getValue()& value)){
				weekdays.add(e);
			}
			
		});
		return weekdays;
	}
	
	public static Integer getWeekday(List<Weekday> weekdays) {
		
		Objects.requireNonNull(weekdays, "weekdays can not be null.");
		Integer count = 0;
		for (Weekday weekday : weekdays){
			count += weekday.getValue();
		}
		return count;
	}
}
